<!-- 广告位编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改自动信配置':'添加自动信配置'"
    :visible="visible"
    width="960px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="170px">
      <el-form-item label="邮件模板" prop="template_id">
        <el-select v-model="form.template_id">
          <el-option v-for="item in config.email_template_list" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="开启自动信的sku" prop="sku_list">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          v-model="form.sku_list"
          placeholder="请输入sku,多个以;或换行分开"/>
      </el-form-item>
      <el-form-item label="接收邮箱:" prop="email_name">
        <el-input
          clearable
          v-model="form.email_name"
          placeholder="请输入邮箱"/>
      </el-form-item>
      <el-form-item label="间隔多久发送" prop="interval_time">
        <el-select v-model="form.interval_time">
          <el-option v-for="item in config.interval_time_list" :key="item.val" :label="item.label" :value="item.val"/>
        </el-select>
      </el-form-item>

    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
  name: 'autoEmailEdit',
  components: {},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    configList: Object,
    // 栏目数据
    cateList: Array
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({
        evaluation_process: 1,
        gift_section:1,
        star_rating_evaluation:1,
        jump_amazon_comment:1,
        jump_amazon_star:4,
      }, this.data),
      config: Object.assign({}, this.configList),
      // 表单验证规则
      rules: {
        template_id: [
          {required: true, message: '请选择邮件模板', trigger: 'blur'}
        ],
        interval_time: [
          {required: true, message: '请选择定时发送', trigger: 'blur'}
        ],
        email_name: [
          {required: true, message: '请填写接收邮箱,否则客户无法回复邮件', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }

    },
    configList() {
      if (this.configList) {
        this.config = Object.assign({}, this.configList);
      } else {
        this.config = {};
      }
    }
  },
  methods: {
    /* 下拉树格式化 */
    normalizer(d) {
      return {
        id: d.id,
        label: d.name,
        children: d.children || undefined
      };
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/autoEmail/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
